package groupon.common.util;

import groupon.commom.model.UserEmail;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class AppUtil {
	
	public static final String EMAIL_PATTERN_STRING = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z]{2,}){1}$)";
	
	private AppUtil(){
	}
	
	public static boolean isNullOrEmpty(String obj){
		if(obj != null && !(obj.trim().isEmpty()) && obj.length() > 0){
			return false;	
		}
		return true;
	}
	
	public static boolean isNullOrEmpty(Object[] collection) {
	        return collection == null || collection.length == 0;
    }
	
    public static boolean isNullOrEmpty(@SuppressWarnings("rawtypes") Collection collection) {
    	System.out.println("heelo>>>" + collection.size());
        return collection == null || collection.size() == 0;
    }
	
	public static String displayDate(Date date, String format) {
	    	//SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy - HH:mm");
	    	SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
    }
	
	public static Date convertStringToDate(String strDate, String format) {
		System.out.println("strDate >>" + strDate);
    	//SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy - HH:mm");
		Date date = null;
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String convertStringToMd5(String input){
        String res = "";
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(input.getBytes());
            byte[] md5 = algorithm.digest();
            String tmp = "";
            for (int i = 0; i < md5.length; i++) {
                tmp = (Integer.toHexString(0xFF & md5[i]));
                if (tmp.length() == 1) {
                    res += "0" + tmp;
                } else {
                    res += tmp;
                }
            }
        } catch (NoSuchAlgorithmException ex) {}
        	return res;
    }
	
	public static boolean checkFormatEmail(String email){
		Pattern pattern = Pattern.compile(EMAIL_PATTERN_STRING);
		return pattern.matcher(email).matches();
	}
	
	public static void sendEmail(final UserEmail user,final String subject){
		ApplicationContext context =  new ClassPathXmlApplicationContext("META-INF/spring-email.xml");
    	GrouponMailSender mailSender = (GrouponMailSender) context.getBean("grouponMailSender");
    	mailSender.sendMail(user, subject);
	}
	
	public static List<String> getPageItemList(){
		List<String> listItem = new ArrayList<String>();
		listItem.add("5");
		listItem.add("10");
		listItem.add("20");
		listItem.add("50");
		return listItem;
		
	}
}
